﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.WriteAPI.Applications.Commands.MeetingReservations;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Meeting.WriteAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MeetingReservationController : ControllerBase
    {
        IMediator mediator;
        ILogger<MeetingReservationController> logger;

        public MeetingReservationController(IMediator mediator, ILogger<MeetingReservationController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 添加会议预约
        /// </summary>
        /// <param name="meetingReservation">会议预约信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddReservation(MeetingReservation meetingReservation)
        {
            try
            {
                AddReservationCommand cmd= new AddReservationCommand
                {
                    Data= meetingReservation
                };
                logger.LogInformation($"添加会议预约成功，会议主题：{meetingReservation.ThemeName}");
                var result=await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("添加会议预约失败",ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改会议预约
        /// </summary>
        /// <param name="meetingReservation">要修改的会议预约信息</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdReservation(MeetingReservation meetingReservation)
        {
            try
            {
                UpdReservationCommand cmd = new UpdReservationCommand
                {
                    Data=meetingReservation
                };
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
